<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/db.php');
class user
{
	private $db;
	private $table_name = 'user';
	public static $power = array(
		'view_CN_forbidden' => 16,
		'view_CN_inattention' => 32,

		'add_film' => 2,
		'edit_film' => 4,
		'add_resource' => 8,
	);
	function __construct()
	{
		$this->db = new db();
	}
	public function insert($data)
	{
		if(isset($data['id'])&&!empty($data['id']))
		{
			$data_for_query['id'] = trim($data['id']);
		}
		if(isset($data['power_sum'])&&!empty($data['power_sum']))
		{
			$data_for_query['power_sum'] = trim($data['power_sum']);
		}
		$sql = "INSERT INTO `".$this->table_name."` (".$this->db->implodeToColumn(array_keys($data_for_query)).") VALUES (".$this->db->implodeToValues(array_values($data_for_query)).")";
		$this->db->query($sql);
		$id = $this->db->lastInsertId();
		if(empty($id))
		{
			return false;
		}
		else
		{
			return $id;
		}
	}
	
	public function select($where,$column='*')
	{
		$sql_1 = "SELECT ".$this->db->implodeToColumn($column)." FROM ".$this->table_name." WHERE ".$this->db->implodeToWhere($where);
		$result_1 = $this->db->query($sql_1);
		$result_1->setFetchMode(PDO::FETCH_ASSOC);
		$fetch_result = $result_1->fetchAll();
		if($fetch_result==false)
		{
			return false;
		}
		else
		{
			return $fetch_result[0];
		}
	}
	
	public function update($where,$data)
	{
		if(isset($data['email'])&&!empty($data['email']))
		{
			$data_for_query['email'] = trim($data['email']);
		}
		if(isset($data['id'])&&!empty($data['id']))
		{
			$data_for_query['id'] = trim($data['id']);
		}
		if(isset($data['name'])&&!empty($data['name']))
		{
			$data_for_query['name'] = trim($data['name']);
		}
		if(isset($data['power_sum'])&&!empty($data['power_sum']))
		{
			$data_for_query['power_sum'] = trim($data['power_sum']);
		}
		if(isset($data['url'])&&!empty($data['url']))
		{
			$data_for_query['url'] = trim($data['url']);
		}
		$sql = "UPDATE `".$this->table_name."` SET ".$this->db->implodeToUpdate($data_for_query)." WHERE ".$this->db->implodeToWhere($where);
		$count = $this->db->query($sql);
		return $count;
	}
	
	public function updateById($id,$data)
	{
		$where = array(
			'id' => $id,
		);
		return $this->update($where,$data);
	}
}

?>
